import { defineStore } from "pinia";
import { applyRider, applyRiderRecord, cancelApplyRider, login, riderInfo, riderOnlineStatus } from "@/apis/user.js";
import {
  bankCardList,
  bindBankcard,
  bindWechat,
  bindWechatInfo,
  supportBankType,
  unbindBankcard,
  unBindWechat,
} from "../apis/user";

export const useUserStore = defineStore("user", {
  state: () => {
    return {
      // 是否登录
      isLogin: false,
      // 登录凭证
      token: null,
      // 是否骑手
      isRider: false,
      // 是否绑定微信
      isBindWechat: false,
      // 骑手信息
      riderInfo: {
        id: 0, //用户ID
        name: "", //用户昵称
        phone: "", //电话
        rider_amount_total: 0, //累计佣金
        rider_amount: 0, //可提现佣金
        today_amount: 0, //今日佣金
        week_amount: 0, //本周佣金
      },
      bankcardList: [],
      wechatInfo: {
        avatar_url: "",
        nickname: "",
      },
    };
  },

  getters: {
    // 是否绑定银行卡
    isBindBankcard(state) {
      return state.bankcardList.length > 0;
    },
  },

  actions: {
    // 登出
    logout() {
      this.isLogin = false;
      this.token = null;
      uni.reLaunch({
        url: "/pages/login/login",
      });
    },
    // 登录
    async fetchLogin(data) {
      const response = await login(data);
      if (response.result.success) {
        this.isLogin = true;
        this.token = response.result.data.token.access_token;
        console.log(this.token);
        await this.fetchRiderInfo();
        await this.fetchIsRider();
        await this.fetchBindWechatInfo();
        uni.reLaunch({
          url: "/pages/index/index",
        });
      } else {
        uni.$u.toast(response.result.message);
      }
    },
    // 获取骑手信息
    async fetchRiderInfo() {
      const res = await riderInfo();
      if (res.result.success) {
        this.riderInfo = res.result.data;
      }
    },
    // 通过骑手在线状态判断用户是否骑手身份
    async fetchIsRider() {
      await riderOnlineStatus({
        action: "online",
      })
        .then(() => {
          // then 已申请骑手
          this.isRider = true;
          return Promise.resolve(true);
        })
        .catch(() => {
          // catch 未申请骑手
          this.isRider = false;
          return Promise.resolve(false);
        });
    },
    // 骑手申请
    async fetchApplyRider(data) {
      const response = await applyRider(data);
      return Promise.resolve(response.result.success);
    },
    // 骑手申请记录
    async fetchApplyRiderRecord(params) {
      const response = await applyRiderRecord(params);
      if (response.result.success) {
        return response.result.data;
      }
      return [];
    },
    // 骑手取消申请
    async fetchCancelApplyRider(data) {
      const response = await cancelApplyRider(data);
      return Promise.resolve(response.result.success);
    },
    // 绑定微信
    async fetchBindWechat(data) {
      const res = await bindWechat(data);
      if (res.result.success) {
        uni.$u.toast("绑定成功");
        await this.fetchBindWechatInfo();
      }
      return Promise.resolve(res.result.success);
    },
    // 解绑微信
    async fetchUnbindWechat() {
      const res = await unBindWechat();
      if (res.result.success) {
        this.isBindWechat = false;
        await this.fetchBindWechatInfo();
        uni.$u.toast("解绑成功");
      }
      return res.result.success;
    },
    // 获取用户绑定得微信信息
    async fetchBindWechatInfo() {
      const res = await bindWechatInfo();
      if (res.result.success) {
        if (res.result.data.is_bind) {
          this.wechatInfo = res.result.data.info;
        }
        this.isBindWechat = res.result.data.is_bind;
      }
      return res.result.success;
    },
    // 获取系统支持的银行卡类型列表
    async fetchSupportBankTypeList() {
      const res = await supportBankType();
      console.log(res);
    },
    // 银行卡列表
    async fetchBankcardList(params) {
      const res = await bankCardList(params);
      if (res.result.success) {
        this.bankcardList = res.result.data;
      }
      return res.result.data;
    },
    // 绑定银行卡
    async fetchBindBankcard(data) {
      const res = await bindBankcard(data);
      if (res.result.success) {
        await this.fetchBankcardList();
        uni.$u.toast("绑定成功");
      }
      return Promise.resolve(res.result.success);
    },
    // 解绑银行卡
    async fetchUnbindBankcard(data) {
      const res = await unbindBankcard(data);
      if (res.result.success) {
        await this.fetchBankcardList();
        uni.$u.toast("解绑成功");
      }
      return Promise.resolve(res.result.success);
    },
  },

  unistorage: true,
});
